
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_STUDENTS 100
#define MAX_SUBJECTS 10
#define MAX_NAME_LEN 11

// 定义学生结构体
typedef struct {
    char name[MAX_NAME_LEN]; // 学生姓名
    int totalScore; // 学生总分
    int scores[MAX_SUBJECTS]; // 存储学生各科成绩的数组
} Student;

// 定义全局变量
int n, m; // n为学生数量，m为科目数量
char subjects[MAX_SUBJECTS][MAX_NAME_LEN]; // 存储科目名称的数组
Student students[MAX_STUDENTS]; // 存储学生对象的数组
char rankSubject[MAX_NAME_LEN]; // 用作排名的科目名称

// 定义比较函数，用于 qsort 函数
int cmp(const void *a, const void *b) {
    Student *studentA = (Student *)a;
    Student *studentB = (Student *)b;
    int scoreA = studentA->totalScore;
    int scoreB = studentB->totalScore;
    if (strcmp(rankSubject, "") != 0) {
        for (int i = 0; i < m; ++i) {
            if (strcmp(rankSubject, subjects[i]) == 0) {
                scoreA = studentA->scores[i];
                scoreB = studentB->scores[i];
                break;
            }
        }
    }
    if (scoreA != scoreB) {
        return scoreB - scoreA; // 成绩高的排前面
    }
    return strcmp(studentA->name, studentB->name); // 成绩相同则按姓名字典序排列
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 0; i < m; ++i) {
        scanf("%s", subjects[i]);
    }

    for (int i = 0; i < n; ++i) {
        scanf("%s", students[i].name);
        students[i].totalScore = 0;
        for (int j = 0; j < m; ++j) {
            scanf("%d", &students[i].scores[j]);
            students[i].totalScore += students[i].scores[j];
        }
    }

    scanf("%s", rankSubject);

    // 对学生进行排序
    qsort(students, n, sizeof(Student), cmp);

    // 输出排序后的学生姓名
    for (int i = 0; i < n; ++i) {
        printf("%s ", students[i].name);
    }
    printf("\n");

    return 0;
}

